import java.util.Arrays;

public class Solution {

        public int[] duplicateZeros(int[] arr) {
            int cur = 0,dest = -1;
            int n = arr.length;
            //先找到最后一个需要复写的数
            while(cur < n){
                if(arr[cur] == 0){
                    dest += 2;
                }else{
                    dest += 1;
                }
                if(dest >= n-1){
                    break;

                }

                cur++;
            }
            //处理一下边界条件
            if(dest == n){
                arr[n -1] = 0;
                cur--;
                dest -= 2;

            }
            //从后先前复写
            while(cur >= 0){
                if(arr[cur] != 0){
                    arr[dest--] = arr[cur--];
                }else{
                    arr[dest--] = 0;
                    arr[dest--] = 0;
                    cur--;
                }
            }
            return arr;

        }

    public static void main(String[] args) {
        Solution solution= new Solution();

        int[] arryr = {1,0,2,3,0,4};
        System.out.println(Arrays.toString(solution.duplicateZeros(arryr)));

    }

}
